package com.peanut.controller;

import cn.hutool.crypto.digest.MD5;
import com.mvc.annotation.*;
import com.peanut.constants.Constants;
import com.peanut.constants.Status;
import com.peanut.entity.AdminUser;
import com.peanut.entity.JsonResult;
import com.peanut.entity.Page;
import com.peanut.service.AdminUserService;
import com.spring.annotation.Autowired;
import com.spring.annotation.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


/**
 * 后台管理员相关的接口
 */

@Controller
public class AdminUserController {
	
	@Autowired
	private AdminUserService adminUserService;
	
	
	/**
	 * 查询所有管理员
	 */
	@RequestMapping(value = "/admin/query.do")
	@ResponseBody
	public JsonResult queryAdminUser(@RequestParam(defaultValue = "1", required = true) Integer currentPage,
									 @RequestParam(defaultValue = "10", required = true) Integer pageSize) {
		if (currentPage < 1) {
			currentPage = 1;
		}
		if (pageSize < 1) {
			pageSize = 10;
		}
		Page<AdminUser> adminUserPage = adminUserService.pageList(currentPage, pageSize);
		JsonResult json = JsonResult.success(adminUserPage);
		return json;
	}
	
	
	/**
	 * 添加管理员
	 */
	@RequestMapping(value = "/admin/add.do")
	@ResponseBody
	public JsonResult addAdminUser(@RequestBody @Valid AdminUser adminUser, HttpServletRequest request) {
		HttpSession session = request.getSession();
		AdminUser loginUser = (AdminUser) session.getAttribute(Constants.LOGIN_USER);
		
		if (loginUser == null) {
			return JsonResult.fail("尚未登录");
		}
		
		adminUser.setCreateUserId(loginUser.getId());
		adminUser.setRegTime(LocalDateTime.now());
		
		String md5Password = MD5.create().digestHex(adminUser.getPassword(), "utf-8");
		adminUser.setPassword(md5Password);
		
		if (adminUserService.save(adminUser)) {
			return JsonResult.success("添加用户成功");
		} else {
			return JsonResult.fail("添加用户失败");
		}
	}
	
	/**
	 * 删除管理员，改标记
	 */
	@RequestMapping(value = "/admin/delete.do")
	@ResponseBody
	public JsonResult deleteAdminUser(@RequestParam(required = true) Integer id, HttpServletRequest request) {
		HttpSession session = request.getSession();
		AdminUser loginUser = (AdminUser) session.getAttribute(Constants.LOGIN_USER);
		if (loginUser == null) {
			return JsonResult.fail("尚未登录");
		}
		if (loginUser.getId().equals(id)) {
			return JsonResult.status(Status.CANNOT_DELETE_ME);
		}
		if (id == 1) {
			return JsonResult.status(Status.CANNOT_DELETE_INNER_ADMIN);
		}
		
		
		if (adminUserService.deleteAdminUser(id)) {
			return JsonResult.success("删除用户成功");
		}
		return JsonResult.fail("删除用户失败");
	}
	
	/**
	 * 修改管理员
	 */
	@RequestMapping(value = "/admin/update.do")
	@ResponseBody
	public JsonResult updateAdminUser(@RequestBody @Valid AdminUser adminUser, HttpServletRequest request) {
		HttpSession session = request.getSession();
		AdminUser loginUser = (AdminUser) session.getAttribute(Constants.LOGIN_USER);
		
		if (adminUser.getId().equals(loginUser.getId()) && adminUser.getIsValid() == 0) {
			return JsonResult.status(Status.CANNOT_INVALID_ME);
		}
		if (adminUser.getId() == 1) {
			return JsonResult.status(Status.CANNOT_INVALID_INNER_ADMIN);
		}
		
		
		if (adminUserService.updateAdminUser(adminUser)) {
			return JsonResult.success("修改用户成功");
		}
		return JsonResult.fail("修改用户失败");
	}
}
